﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Configuration;
using Model;
using Utility.ExtensionFunc;
namespace DAL
{
    public class RestaurantRepository : RepositoryBase
    {
        private static  RestaurantRepository restRepo;

        private RestaurantRepository()
        {
            xmlDoc = new XmlDocument();
            xmlPath = basicPath + ConfigurationManager.AppSettings["RestaurantPath"].ToString();
        }

        public static  RestaurantRepository Instrances
        {
            get
            {
                if (null == restRepo) { restRepo = new RestaurantRepository(); }
                return restRepo;
            }
        }

        public List<RESTAURANT> LoadRestaurantXML()
        {
            lock (thisLock)
            {
                List<RESTAURANT> restaurantList = new List<RESTAURANT>();
                this.LoadXML();
                restaurantList = xmlRoot.ConvertToEntity<RESTAURANT>(RESTAURANT.NODE);
                return restaurantList;
            }
        }

        public List<MENU> LoadMenuXML(string restaurantName)
        {
            lock (thisLock)
            {
                List<MENU> menuList = new List<MENU>();
                this.LoadXML();
                foreach (XmlElement node in xmlRoot.ChildNodes)
                {
                    if (node.SelectSingleNode(RESTAURANT.NODE_NAME).InnerText != restaurantName)
                    {
                        continue;
                    }

                    menuList = node.SelectSingleNode(RESTAURANT.NODE_MENU_LIST).ConvertToEntity<MENU>(MENU.NODE);
                }

                return menuList;
            }
        }

    }
}
